﻿using G.Data;
using Sample.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “基本页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234237 上有介绍

namespace Sample.View
{
    /// <summary>
    /// 基本页，提供大多数应用程序通用的特性。
    /// </summary>
    public sealed partial class PageListPage : Sample.Common.LayoutAwarePage
    {
        public PageListPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// 使用在导航过程中传递的内容填充页。在从以前的会话
        /// 重新创建页时，也会提供任何已保存状态。
        /// </summary>
        /// <param name="navigationParameter">最初请求此页时传递给
        /// <see cref="Frame.Navigate(Type, Object)"/> 的参数值。
        /// </param>
        /// <param name="pageState">此页在以前会话期间保留的状态
        /// 字典。首次访问页面时为 null。</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            PDataSource = new DataSource();
            PDataSource.IsLoadingDataChanged += PDataSource_IsLoadingDataChanged;
            gridView.ItemsSource = PDataSource;
        }

        void PDataSource_IsLoadingDataChanged(object sender, IsLoadingDataChangedEventArgs e)
        {
            progress.IsActive = e.NewIsLoadingData;
        }

        DataSource PDataSource;
        /// <summary>
        /// 保留与此页关联的状态，以防挂起应用程序或
        /// 从导航缓存中放弃此页。值必须符合
        /// <see cref="SuspensionManager.SessionState"/> 的序列化要求。
        /// </summary>
        /// <param name="pageState">要使用可序列化状态填充的空字典。</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }

        private void girdView_ItemClick_1(object sender, ItemClickEventArgs e)
        {
            PDataSource.Remove(e.ClickedItem as SampleDataItem);
        }
    }
    public class DataSource : G.Data.IncrementalLoadingCollection<SampleDataItem>
    {
        int count = 0;
        public override async System.Threading.Tasks.Task<G.Data.IncrementalLoadingResult<SampleDataItem>> PullDataAsync(uint currentPageLowerBound, uint currentPageUpperBound)
        {
            IncrementalLoadingResult<SampleDataItem> result = null;
            String ITEM_CONTENT = String.Format("Item Content: {0}\n\n{0}\n\n{0}\n\n{0}\n\n{0}\n\n{0}\n\n{0}",
                   "Curabitur class aliquam vestibulum nam curae maecenas sed integer cras phasellus suspendisse quisque donec dis praesent accumsan bibendum pellentesque condimentum adipiscing etiam consequat vivamus dictumst aliquam duis convallis scelerisque est parturient ullamcorper aliquet fusce suspendisse nunc hac eleifend amet blandit facilisi condimentum commodo scelerisque faucibus aenean ullamcorper ante mauris dignissim consectetuer nullam lorem vestibulum habitant conubia elementum pellentesque morbi facilisis arcu sollicitudin diam cubilia aptent vestibulum auctor eget dapibus pellentesque inceptos leo egestas interdum nulla consectetuer suspendisse adipiscing pellentesque proin lobortis sollicitudin augue elit mus congue fermentum parturient fringilla euismod feugiat");
            var group1 = new SampleDataGroup("Group-1",
                 "Group Title: 1",
                 "Group Subtitle: 1",
                 "Assets/DarkGray.png",
                 "Group Description: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tempor scelerisque lorem in vehicula. Aliquam tincidunt, lacus ut sagittis tristique, turpis massa volutpat augue, eu rutrum ligula ante a ante");
            int t = count + 14;
            for (; count <= t; count++)
            {
                group1.Items.Add(new SampleDataItem("Group-1-Item-" + count,
                  "Item Title: " + count,
                  "Item Subtitle: " + count,
                  "Assets/LightGray.png",
                  "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat.",
                  ITEM_CONTENT,
                  group1));
            }
            result = new IncrementalLoadingResult<SampleDataItem>(group1.Items.ToArray(), 14);
            await System.Threading.ThreadExtention.LazyInvok(TimeSpan.FromSeconds(1.5), null);//延迟1.5秒
            return result;
        }

        public override void Dispose()
        {

        }
    }
}
